<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>avltree.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_structs_avltree.js.html">avltree.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2007 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Datastructure: AvlTree.
<a name="line17"></a> *
<a name="line18"></a> *
<a name="line19"></a> * This file provides the implementation of an AVL-Tree datastructure. The tree
<a name="line20"></a> * maintains a set of unique values in a sorted order. The values can be
<a name="line21"></a> * accessed efficiently in their sorted order since the tree enforces an O(logn)
<a name="line22"></a> * maximum height. See http://en.wikipedia.org/wiki/Avl_tree for more detail.
<a name="line23"></a> *
<a name="line24"></a> * The big-O notation for all operations are below:
<a name="line25"></a> * &lt;pre&gt;
<a name="line26"></a> *   Method                 big-O
<a name="line27"></a> * ----------------------------------------------------------------------------
<a name="line28"></a> * - add                    O(logn)
<a name="line29"></a> * - remove                 O(logn)
<a name="line30"></a> * - clear                  O(1)
<a name="line31"></a> * - contains               O(logn)
<a name="line32"></a> * - getCount               O(1)
<a name="line33"></a> * - getMinimum             O(1), or O(logn) when optional root is specified
<a name="line34"></a> * - getMaximum             O(1), or O(logn) when optional root is specified
<a name="line35"></a> * - getHeight              O(1)
<a name="line36"></a> * - getValues              O(n)
<a name="line37"></a> * - inOrderTraverse        O(logn + k), where k is number of traversed nodes
<a name="line38"></a> * - reverseOrderTraverse   O(logn + k), where k is number of traversed nodes
<a name="line39"></a> * &lt;/pre&gt;
<a name="line40"></a> */
<a name="line41"></a>
<a name="line42"></a>
<a name="line43"></a>goog.provide(&#39;goog.structs.AvlTree&#39;);
<a name="line44"></a>goog.provide(&#39;goog.structs.AvlTree.Node&#39;);
<a name="line45"></a>
<a name="line46"></a>goog.require(&#39;goog.structs&#39;);
<a name="line47"></a>goog.require(&#39;goog.structs.Collection&#39;);
<a name="line48"></a>
<a name="line49"></a>
<a name="line50"></a>
<a name="line51"></a>/**
<a name="line52"></a> * Constructs an AVL-Tree, which uses the specified comparator to order its
<a name="line53"></a> * values. The values can be accessed efficiently in their sorted order since
<a name="line54"></a> * the tree enforces a O(logn) maximum height.
<a name="line55"></a> *
<a name="line56"></a> * @param {Function=} opt_comparator Function used to order the tree&#39;s nodes.
<a name="line57"></a> * @constructor
<a name="line58"></a> * @implements {goog.structs.Collection}
<a name="line59"></a> */
<a name="line60"></a>goog.structs.AvlTree = function(opt_comparator) {
<a name="line61"></a>  this.comparator_ = opt_comparator ||
<a name="line62"></a>                     goog.structs.AvlTree.DEFAULT_COMPARATOR_;
<a name="line63"></a>};
<a name="line64"></a>
<a name="line65"></a>
<a name="line66"></a>/**
<a name="line67"></a> * String comparison function used to compare values in the tree. This function
<a name="line68"></a> * is used by default if no comparator is specified in the tree&#39;s constructor.
<a name="line69"></a> *
<a name="line70"></a> * @param {string} a The first string.
<a name="line71"></a> * @param {string} b The second string.
<a name="line72"></a> * @return {number} -1 if a &lt; b, 1 if a &gt; b, 0 if a = b.
<a name="line73"></a> * @private
<a name="line74"></a> */
<a name="line75"></a>goog.structs.AvlTree.DEFAULT_COMPARATOR_ = function(a, b) {
<a name="line76"></a>  if (String(a) &lt; String(b)) {
<a name="line77"></a>    return -1;
<a name="line78"></a>  } else if (String(a) &gt; String(b)) {
<a name="line79"></a>    return 1;
<a name="line80"></a>  }
<a name="line81"></a>  return 0;
<a name="line82"></a>};
<a name="line83"></a>
<a name="line84"></a>
<a name="line85"></a>/**
<a name="line86"></a> * Pointer to the root node of the tree.
<a name="line87"></a> *
<a name="line88"></a> * @type {goog.structs.AvlTree.Node}
<a name="line89"></a> * @private
<a name="line90"></a> */
<a name="line91"></a>goog.structs.AvlTree.prototype.root_ = null;
<a name="line92"></a>
<a name="line93"></a>
<a name="line94"></a>/**
<a name="line95"></a> * Comparison function used to compare values in the tree. This function should
<a name="line96"></a> * take two values, a and b, and return x where:
<a name="line97"></a> * &lt;pre&gt;
<a name="line98"></a> *  x &lt; 0 if a &lt; b,
<a name="line99"></a> *  x &gt; 0 if a &gt; b,
<a name="line100"></a> *  x = 0 otherwise
<a name="line101"></a> * &lt;/pre&gt;
<a name="line102"></a> *
<a name="line103"></a> * @type {Function}
<a name="line104"></a> * @private
<a name="line105"></a> */
<a name="line106"></a>goog.structs.AvlTree.prototype.comparator_ = null;
<a name="line107"></a>
<a name="line108"></a>
<a name="line109"></a>/**
<a name="line110"></a> * Pointer to the node with the smallest value in the tree.
<a name="line111"></a> *
<a name="line112"></a> * @type {goog.structs.AvlTree.Node}
<a name="line113"></a> * @private
<a name="line114"></a> */
<a name="line115"></a>goog.structs.AvlTree.prototype.minNode_ = null;
<a name="line116"></a>
<a name="line117"></a>
<a name="line118"></a>/**
<a name="line119"></a> * Pointer to the node with the largest value in the tree.
<a name="line120"></a> *
<a name="line121"></a> * @type {goog.structs.AvlTree.Node}
<a name="line122"></a> * @private
<a name="line123"></a> */
<a name="line124"></a>goog.structs.AvlTree.prototype.maxNode_ = null;
<a name="line125"></a>
<a name="line126"></a>
<a name="line127"></a>/**
<a name="line128"></a> * Inserts a node into the tree with the specified value if the tree does
<a name="line129"></a> * not already contain a node with the specified value. If the value is
<a name="line130"></a> * inserted, the tree is balanced to enforce the AVL-Tree height property.
<a name="line131"></a> *
<a name="line132"></a> * @param {*} value Value to insert into the tree.
<a name="line133"></a> * @return {boolean} Whether value was inserted into the tree.
<a name="line134"></a> * @override
<a name="line135"></a> */
<a name="line136"></a>goog.structs.AvlTree.prototype.add = function(value) {
<a name="line137"></a>  // If the tree is empty, create a root node with the specified value
<a name="line138"></a>  if (this.root_ == null) {
<a name="line139"></a>    this.root_ = new goog.structs.AvlTree.Node(value);
<a name="line140"></a>    this.minNode_ = this.root_;
<a name="line141"></a>    this.maxNode_ = this.root_;
<a name="line142"></a>    return true;
<a name="line143"></a>  }
<a name="line144"></a>
<a name="line145"></a>  // This will be set to the new node if a new node is added.
<a name="line146"></a>  var newNode = null;
<a name="line147"></a>
<a name="line148"></a>  // Depth traverse the tree and insert the value if we reach a null node
<a name="line149"></a>  this.traverse_(function(node) {
<a name="line150"></a>    var retNode = null;
<a name="line151"></a>    if (this.comparator_(node.value, value) &gt; 0) {
<a name="line152"></a>      retNode = node.left;
<a name="line153"></a>      if (node.left == null) {
<a name="line154"></a>        newNode = new goog.structs.AvlTree.Node(value, node);
<a name="line155"></a>        node.left = newNode;
<a name="line156"></a>        if (node == this.minNode_) {
<a name="line157"></a>          this.minNode_ = newNode;
<a name="line158"></a>        }
<a name="line159"></a>      }
<a name="line160"></a>    } else if (this.comparator_(node.value, value) &lt; 0) {
<a name="line161"></a>      retNode = node.right;
<a name="line162"></a>      if (node.right == null) {
<a name="line163"></a>        newNode = new goog.structs.AvlTree.Node(value, node);
<a name="line164"></a>        node.right = newNode;
<a name="line165"></a>        if (node == this.maxNode_) {
<a name="line166"></a>          this.maxNode_ = newNode;
<a name="line167"></a>        }
<a name="line168"></a>      }
<a name="line169"></a>    }
<a name="line170"></a>    return retNode; // If null, we&#39;ll stop traversing the tree
<a name="line171"></a>  });
<a name="line172"></a>
<a name="line173"></a>  // If a node was added, increment counts and balance tree.
<a name="line174"></a>  if (newNode) {
<a name="line175"></a>    this.traverse_(
<a name="line176"></a>        function(node) {
<a name="line177"></a>          node.count++;
<a name="line178"></a>          return node.parent;
<a name="line179"></a>        },
<a name="line180"></a>        newNode.parent);
<a name="line181"></a>    this.balance_(newNode.parent); // Maintain the AVL-tree balance
<a name="line182"></a>  }
<a name="line183"></a>
<a name="line184"></a>  // Return true if a node was added, false otherwise
<a name="line185"></a>  return !!newNode;
<a name="line186"></a>};
<a name="line187"></a>
<a name="line188"></a>
<a name="line189"></a>/**
<a name="line190"></a> * Removes a node from the tree with the specified value if the tree contains a
<a name="line191"></a> * node with this value. If a node is removed the tree is balanced to enforce
<a name="line192"></a> * the AVL-Tree height property. The value of the removed node is returned.
<a name="line193"></a> *
<a name="line194"></a> * @param {*} value Value to find and remove from the tree.
<a name="line195"></a> * @return {*} The value of the removed node or null if the value was not in
<a name="line196"></a> *     the tree.
<a name="line197"></a> * @override
<a name="line198"></a> */
<a name="line199"></a>goog.structs.AvlTree.prototype.remove = function(value) {
<a name="line200"></a>  // Assume the value is not removed and set the value when it is removed
<a name="line201"></a>  var retValue = null;
<a name="line202"></a>
<a name="line203"></a>  // Depth traverse the tree and remove the value if we find it
<a name="line204"></a>  this.traverse_(function(node) {
<a name="line205"></a>    var retNode = null;
<a name="line206"></a>    if (this.comparator_(node.value, value) &gt; 0) {
<a name="line207"></a>      retNode = node.left;
<a name="line208"></a>    } else if (this.comparator_(node.value, value) &lt; 0) {
<a name="line209"></a>      retNode = node.right;
<a name="line210"></a>    } else {
<a name="line211"></a>      retValue = node.value;
<a name="line212"></a>      this.removeNode_(node);
<a name="line213"></a>    }
<a name="line214"></a>    return retNode; // If null, we&#39;ll stop traversing the tree
<a name="line215"></a>  });
<a name="line216"></a>
<a name="line217"></a>  // Return the value that was removed, null if the value was not in the tree
<a name="line218"></a>  return retValue;
<a name="line219"></a>};
<a name="line220"></a>
<a name="line221"></a>
<a name="line222"></a>/**
<a name="line223"></a> * Removes all nodes from the tree.
<a name="line224"></a> */
<a name="line225"></a>goog.structs.AvlTree.prototype.clear = function() {
<a name="line226"></a>  this.root_ = null;
<a name="line227"></a>  this.minNode_ = null;
<a name="line228"></a>  this.maxNode_ = null;
<a name="line229"></a>};
<a name="line230"></a>
<a name="line231"></a>
<a name="line232"></a>/**
<a name="line233"></a> * Returns true if the tree contains a node with the specified value, false
<a name="line234"></a> * otherwise.
<a name="line235"></a> *
<a name="line236"></a> * @param {*} value Value to find in the tree.
<a name="line237"></a> * @return {boolean} Whether the tree contains a node with the specified value.
<a name="line238"></a> * @override
<a name="line239"></a> */
<a name="line240"></a>goog.structs.AvlTree.prototype.contains = function(value) {
<a name="line241"></a>  // Assume the value is not in the tree and set this value if it is found
<a name="line242"></a>  var isContained = false;
<a name="line243"></a>
<a name="line244"></a>  // Depth traverse the tree and set isContained if we find the node
<a name="line245"></a>  this.traverse_(function(node) {
<a name="line246"></a>    var retNode = null;
<a name="line247"></a>    if (this.comparator_(node.value, value) &gt; 0) {
<a name="line248"></a>      retNode = node.left;
<a name="line249"></a>    } else if (this.comparator_(node.value, value) &lt; 0) {
<a name="line250"></a>      retNode = node.right;
<a name="line251"></a>    } else {
<a name="line252"></a>      isContained = true;
<a name="line253"></a>    }
<a name="line254"></a>    return retNode; // If null, we&#39;ll stop traversing the tree
<a name="line255"></a>  });
<a name="line256"></a>
<a name="line257"></a>  // Return true if the value is contained in the tree, false otherwise
<a name="line258"></a>  return isContained;
<a name="line259"></a>};
<a name="line260"></a>
<a name="line261"></a>
<a name="line262"></a>/**
<a name="line263"></a> * Returns the number of values stored in the tree.
<a name="line264"></a> *
<a name="line265"></a> * @return {number} The number of values stored in the tree.
<a name="line266"></a> * @override
<a name="line267"></a> */
<a name="line268"></a>goog.structs.AvlTree.prototype.getCount = function() {
<a name="line269"></a>  return this.root_ ? this.root_.count : 0;
<a name="line270"></a>};
<a name="line271"></a>
<a name="line272"></a>
<a name="line273"></a>/**
<a name="line274"></a> * Returns a k-th smallest value, based on the comparator, where 0 &lt;= k &lt;
<a name="line275"></a> * this.getCount().
<a name="line276"></a> * @param {number} k The number k.
<a name="line277"></a> * @return {*} The k-th smallest value.
<a name="line278"></a> */
<a name="line279"></a>goog.structs.AvlTree.prototype.getKthValue = function(k) {
<a name="line280"></a>  if (k &lt; 0 || k &gt;= this.getCount()) {
<a name="line281"></a>    return null;
<a name="line282"></a>  }
<a name="line283"></a>  return this.getKthNode_(k).value;
<a name="line284"></a>};
<a name="line285"></a>
<a name="line286"></a>
<a name="line287"></a>/**
<a name="line288"></a> * Returns the value u, such that u is contained in the tree and u &lt; v, for all
<a name="line289"></a> * values v in the tree where v != u.
<a name="line290"></a> *
<a name="line291"></a> * @return {*} The minimum value contained in the tree.
<a name="line292"></a> */
<a name="line293"></a>goog.structs.AvlTree.prototype.getMinimum = function() {
<a name="line294"></a>  return this.getMinNode_().value;
<a name="line295"></a>};
<a name="line296"></a>
<a name="line297"></a>
<a name="line298"></a>/**
<a name="line299"></a> * Returns the value u, such that u is contained in the tree and u &gt; v, for all
<a name="line300"></a> * values v in the tree where v != u.
<a name="line301"></a> *
<a name="line302"></a> * @return {*} The maximum value contained in the tree.
<a name="line303"></a> */
<a name="line304"></a>goog.structs.AvlTree.prototype.getMaximum = function() {
<a name="line305"></a>  return this.getMaxNode_().value;
<a name="line306"></a>};
<a name="line307"></a>
<a name="line308"></a>
<a name="line309"></a>/**
<a name="line310"></a> * Returns the height of the tree (the maximum depth). This height should
<a name="line311"></a> * always be &lt;= 1.4405*(Math.log(n+2)/Math.log(2))-1.3277, where n is the
<a name="line312"></a> * number of nodes in the tree.
<a name="line313"></a> *
<a name="line314"></a> * @return {number} The height of the tree.
<a name="line315"></a> */
<a name="line316"></a>goog.structs.AvlTree.prototype.getHeight = function() {
<a name="line317"></a>  return this.root_ ? this.root_.height : 0;
<a name="line318"></a>};
<a name="line319"></a>
<a name="line320"></a>
<a name="line321"></a>/**
<a name="line322"></a> * Inserts the values stored in the tree into a new Array and returns the Array.
<a name="line323"></a> *
<a name="line324"></a> * @return {Array} An array containing all of the trees values in sorted order.
<a name="line325"></a> */
<a name="line326"></a>goog.structs.AvlTree.prototype.getValues = function() {
<a name="line327"></a>  var ret = [];
<a name="line328"></a>  this.inOrderTraverse(function(value) {
<a name="line329"></a>    ret.push(value);
<a name="line330"></a>  });
<a name="line331"></a>  return ret;
<a name="line332"></a>};
<a name="line333"></a>
<a name="line334"></a>
<a name="line335"></a>/**
<a name="line336"></a> * Performs an in-order traversal of the tree and calls {@code func} with each
<a name="line337"></a> * traversed node, optionally starting from the smallest node with a value &gt;= to
<a name="line338"></a> * the specified start value. The traversal ends after traversing the tree&#39;s
<a name="line339"></a> * maximum node or when {@code func} returns a value that evaluates to true.
<a name="line340"></a> *
<a name="line341"></a> * @param {Function} func Function to call on each traversed node.
<a name="line342"></a> * @param {Object=} opt_startValue If specified, traversal will begin on the
<a name="line343"></a> *    node with the smallest value &gt;= opt_startValue.
<a name="line344"></a> */
<a name="line345"></a>goog.structs.AvlTree.prototype.inOrderTraverse =
<a name="line346"></a>    function(func, opt_startValue) {
<a name="line347"></a>  // If our tree is empty, return immediately
<a name="line348"></a>  if (!this.root_) {
<a name="line349"></a>    return;
<a name="line350"></a>  }
<a name="line351"></a>
<a name="line352"></a>  // Depth traverse the tree to find node to begin in-order traversal from
<a name="line353"></a>  var startNode;
<a name="line354"></a>  if (opt_startValue) {
<a name="line355"></a>    this.traverse_(function(node) {
<a name="line356"></a>      var retNode = null;
<a name="line357"></a>      if (this.comparator_(node.value, opt_startValue) &gt; 0) {
<a name="line358"></a>        retNode = node.left;
<a name="line359"></a>        startNode = node;
<a name="line360"></a>      } else if (this.comparator_(node.value, opt_startValue) &lt; 0) {
<a name="line361"></a>        retNode = node.right;
<a name="line362"></a>      } else {
<a name="line363"></a>        startNode = node;
<a name="line364"></a>      }
<a name="line365"></a>      return retNode; // If null, we&#39;ll stop traversing the tree
<a name="line366"></a>    });
<a name="line367"></a>  } else {
<a name="line368"></a>    startNode = this.getMinNode_();
<a name="line369"></a>  }
<a name="line370"></a>
<a name="line371"></a>  // Traverse the tree and call func on each traversed node&#39;s value
<a name="line372"></a>  var node = startNode, prev = startNode.left ? startNode.left : startNode;
<a name="line373"></a>  while (node != null) {
<a name="line374"></a>    if (node.left != null &amp;&amp; node.left != prev &amp;&amp; node.right != prev) {
<a name="line375"></a>      node = node.left;
<a name="line376"></a>    } else {
<a name="line377"></a>      if (node.right != prev) {
<a name="line378"></a>        if (func(node.value)) {
<a name="line379"></a>          return;
<a name="line380"></a>        }
<a name="line381"></a>      }
<a name="line382"></a>      var temp = node;
<a name="line383"></a>      node = node.right != null &amp;&amp; node.right != prev ?
<a name="line384"></a>             node.right :
<a name="line385"></a>             node.parent;
<a name="line386"></a>      prev = temp;
<a name="line387"></a>    }
<a name="line388"></a>  }
<a name="line389"></a>};
<a name="line390"></a>
<a name="line391"></a>
<a name="line392"></a>/**
<a name="line393"></a> * Performs a reverse-order traversal of the tree and calls {@code func} with
<a name="line394"></a> * each traversed node, optionally starting from the largest node with a value
<a name="line395"></a> * &lt;= to the specified start value. The traversal ends after traversing the
<a name="line396"></a> * tree&#39;s minimum node or when func returns a value that evaluates to true.
<a name="line397"></a> *
<a name="line398"></a> * @param {Function} func Function to call on each traversed node.
<a name="line399"></a> * @param {Object=} opt_startValue If specified, traversal will begin on the
<a name="line400"></a> *    node with the largest value &lt;= opt_startValue.
<a name="line401"></a> */
<a name="line402"></a>goog.structs.AvlTree.prototype.reverseOrderTraverse =
<a name="line403"></a>    function(func, opt_startValue) {
<a name="line404"></a>  // If our tree is empty, return immediately
<a name="line405"></a>  if (!this.root_) {
<a name="line406"></a>    return;
<a name="line407"></a>  }
<a name="line408"></a>
<a name="line409"></a>  // Depth traverse the tree to find node to begin reverse-order traversal from
<a name="line410"></a>  var startNode;
<a name="line411"></a>  if (opt_startValue) {
<a name="line412"></a>    this.traverse_(goog.bind(function(node) {
<a name="line413"></a>      var retNode = null;
<a name="line414"></a>      if (this.comparator_(node.value, opt_startValue) &gt; 0) {
<a name="line415"></a>        retNode = node.left;
<a name="line416"></a>      } else if (this.comparator_(node.value, opt_startValue) &lt; 0) {
<a name="line417"></a>        retNode = node.right;
<a name="line418"></a>        startNode = node;
<a name="line419"></a>      } else {
<a name="line420"></a>        startNode = node;
<a name="line421"></a>      }
<a name="line422"></a>      return retNode; // If null, we&#39;ll stop traversing the tree
<a name="line423"></a>    }, this));
<a name="line424"></a>  } else {
<a name="line425"></a>    startNode = this.getMaxNode_();
<a name="line426"></a>  }
<a name="line427"></a>
<a name="line428"></a>  // Traverse the tree and call func on each traversed node&#39;s value
<a name="line429"></a>  var node = startNode, prev = startNode.right ? startNode.right : startNode;
<a name="line430"></a>  while (node != null) {
<a name="line431"></a>    if (node.right != null &amp;&amp; node.right != prev &amp;&amp; node.left != prev) {
<a name="line432"></a>      node = node.right;
<a name="line433"></a>    } else {
<a name="line434"></a>      if (node.left != prev) {
<a name="line435"></a>        if (func(node.value)) {
<a name="line436"></a>          return;
<a name="line437"></a>        }
<a name="line438"></a>      }
<a name="line439"></a>      var temp = node;
<a name="line440"></a>      node = node.left != null &amp;&amp; node.left != prev ?
<a name="line441"></a>             node.left :
<a name="line442"></a>             node.parent;
<a name="line443"></a>      prev = temp;
<a name="line444"></a>    }
<a name="line445"></a>  }
<a name="line446"></a>};
<a name="line447"></a>
<a name="line448"></a>
<a name="line449"></a>/**
<a name="line450"></a> * Performs a traversal defined by the supplied {@code traversalFunc}. The first
<a name="line451"></a> * call to {@code traversalFunc} is passed the root or the optionally specified
<a name="line452"></a> * startNode. After that, calls {@code traversalFunc} with the node returned
<a name="line453"></a> * by the previous call to {@code traversalFunc} until {@code traversalFunc}
<a name="line454"></a> * returns null or the optionally specified endNode. The first call to
<a name="line455"></a> * traversalFunc is passed the root or the optionally specified startNode.
<a name="line456"></a> *
<a name="line457"></a> * @param {Function} traversalFunc Function used to traverse the tree. Takes a
<a name="line458"></a> *     node as a parameter and returns a node.
<a name="line459"></a> * @param {goog.structs.AvlTree.Node=} opt_startNode The node at which the
<a name="line460"></a> *     traversal begins.
<a name="line461"></a> * @param {goog.structs.AvlTree.Node=} opt_endNode The node at which the
<a name="line462"></a> *     traversal ends.
<a name="line463"></a> * @private
<a name="line464"></a> */
<a name="line465"></a>goog.structs.AvlTree.prototype.traverse_ =
<a name="line466"></a>    function(traversalFunc, opt_startNode, opt_endNode) {
<a name="line467"></a>  var node = opt_startNode ? opt_startNode : this.root_;
<a name="line468"></a>  var endNode = opt_endNode ? opt_endNode : null;
<a name="line469"></a>  while (node &amp;&amp; node != endNode) {
<a name="line470"></a>    node = traversalFunc.call(this, node);
<a name="line471"></a>  }
<a name="line472"></a>};
<a name="line473"></a>
<a name="line474"></a>
<a name="line475"></a>/**
<a name="line476"></a> * Ensures that the specified node and all its ancestors are balanced. If they
<a name="line477"></a> * are not, performs left and right tree rotations to achieve a balanced
<a name="line478"></a> * tree. This method assumes that at most 2 rotations are necessary to balance
<a name="line479"></a> * the tree (which is true for AVL-trees that are balanced after each node is
<a name="line480"></a> * added or removed).
<a name="line481"></a> *
<a name="line482"></a> * @param {goog.structs.AvlTree.Node} node Node to begin balance from.
<a name="line483"></a> * @private
<a name="line484"></a> */
<a name="line485"></a>goog.structs.AvlTree.prototype.balance_ = function(node) {
<a name="line486"></a>
<a name="line487"></a>  this.traverse_(function(node) {
<a name="line488"></a>    // Calculate the left and right node&#39;s heights
<a name="line489"></a>    var lh = node.left ? node.left.height : 0;
<a name="line490"></a>    var rh = node.right ? node.right.height : 0;
<a name="line491"></a>
<a name="line492"></a>    // Rotate tree rooted at this node if it is not AVL-tree balanced
<a name="line493"></a>    if (lh - rh &gt; 1) {
<a name="line494"></a>      if (node.left.right &amp;&amp; (!node.left.left ||
<a name="line495"></a>          node.left.left.height &lt; node.left.right.height)) {
<a name="line496"></a>        this.leftRotate_(node.left);
<a name="line497"></a>      }
<a name="line498"></a>      this.rightRotate_(node);
<a name="line499"></a>    } else if (rh - lh &gt; 1) {
<a name="line500"></a>      if (node.right.left &amp;&amp; (!node.right.right ||
<a name="line501"></a>          node.right.right.height &lt; node.right.left.height)) {
<a name="line502"></a>        this.rightRotate_(node.right);
<a name="line503"></a>      }
<a name="line504"></a>      this.leftRotate_(node);
<a name="line505"></a>    }
<a name="line506"></a>
<a name="line507"></a>    // Recalculate the left and right node&#39;s heights
<a name="line508"></a>    lh = node.left ? node.left.height : 0;
<a name="line509"></a>    rh = node.right ? node.right.height : 0;
<a name="line510"></a>
<a name="line511"></a>    // Set this node&#39;s height
<a name="line512"></a>    node.height = Math.max(lh, rh) + 1;
<a name="line513"></a>
<a name="line514"></a>    // Traverse up tree and balance parent
<a name="line515"></a>    return node.parent;
<a name="line516"></a>  }, node);
<a name="line517"></a>
<a name="line518"></a>};
<a name="line519"></a>
<a name="line520"></a>
<a name="line521"></a>/**
<a name="line522"></a> * Performs a left tree rotation on the specified node.
<a name="line523"></a> *
<a name="line524"></a> * @param {goog.structs.AvlTree.Node} node Pivot node to rotate from.
<a name="line525"></a> * @private
<a name="line526"></a> */
<a name="line527"></a>goog.structs.AvlTree.prototype.leftRotate_ = function(node) {
<a name="line528"></a>  // Re-assign parent-child references for the parent of the node being removed
<a name="line529"></a>  if (node.isLeftChild()) {
<a name="line530"></a>    node.parent.left = node.right;
<a name="line531"></a>    node.right.parent = node.parent;
<a name="line532"></a>  } else if (node.isRightChild()) {
<a name="line533"></a>    node.parent.right = node.right;
<a name="line534"></a>    node.right.parent = node.parent;
<a name="line535"></a>  } else {
<a name="line536"></a>    this.root_ = node.right;
<a name="line537"></a>    this.root_.parent = null;
<a name="line538"></a>  }
<a name="line539"></a>
<a name="line540"></a>  // Re-assign parent-child references for the child of the node being removed
<a name="line541"></a>  var temp = node.right;
<a name="line542"></a>  node.right = node.right.left;
<a name="line543"></a>  if (node.right != null) node.right.parent = node;
<a name="line544"></a>  temp.left = node;
<a name="line545"></a>  node.parent = temp;
<a name="line546"></a>
<a name="line547"></a>  // Update counts.
<a name="line548"></a>  temp.count = node.count;
<a name="line549"></a>  node.count -= (temp.right ? temp.right.count : 0) + 1;
<a name="line550"></a>};
<a name="line551"></a>
<a name="line552"></a>
<a name="line553"></a>/**
<a name="line554"></a> * Performs a right tree rotation on the specified node.
<a name="line555"></a> *
<a name="line556"></a> * @param {goog.structs.AvlTree.Node} node Pivot node to rotate from.
<a name="line557"></a> * @private
<a name="line558"></a> */
<a name="line559"></a>goog.structs.AvlTree.prototype.rightRotate_ = function(node) {
<a name="line560"></a>  // Re-assign parent-child references for the parent of the node being removed
<a name="line561"></a>  if (node.isLeftChild()) {
<a name="line562"></a>    node.parent.left = node.left;
<a name="line563"></a>    node.left.parent = node.parent;
<a name="line564"></a>  } else if (node.isRightChild()) {
<a name="line565"></a>    node.parent.right = node.left;
<a name="line566"></a>    node.left.parent = node.parent;
<a name="line567"></a>  } else {
<a name="line568"></a>    this.root_ = node.left;
<a name="line569"></a>    this.root_.parent = null;
<a name="line570"></a>  }
<a name="line571"></a>
<a name="line572"></a>  // Re-assign parent-child references for the child of the node being removed
<a name="line573"></a>  var temp = node.left;
<a name="line574"></a>  node.left = node.left.right;
<a name="line575"></a>  if (node.left != null) node.left.parent = node;
<a name="line576"></a>  temp.right = node;
<a name="line577"></a>  node.parent = temp;
<a name="line578"></a>
<a name="line579"></a>  // Update counts.
<a name="line580"></a>  temp.count = node.count;
<a name="line581"></a>  node.count -= (temp.left ? temp.left.count : 0) + 1;
<a name="line582"></a>};
<a name="line583"></a>
<a name="line584"></a>
<a name="line585"></a>/**
<a name="line586"></a> * Removes the specified node from the tree and ensures the tree still
<a name="line587"></a> * maintains the AVL-tree balance.
<a name="line588"></a> *
<a name="line589"></a> * @param {goog.structs.AvlTree.Node} node The node to be removed.
<a name="line590"></a> * @private
<a name="line591"></a> */
<a name="line592"></a>goog.structs.AvlTree.prototype.removeNode_ = function(node) {
<a name="line593"></a>  // Perform normal binary tree node removal, but balance the tree, starting
<a name="line594"></a>  // from where we removed the node
<a name="line595"></a>  if (node.left != null || node.right != null) {
<a name="line596"></a>    var b = null; // Node to begin balance from
<a name="line597"></a>    var r;        // Node to replace the node being removed
<a name="line598"></a>    if (node.left != null) {
<a name="line599"></a>      r = this.getMaxNode_(node.left);
<a name="line600"></a>
<a name="line601"></a>      // Update counts.
<a name="line602"></a>      this.traverse_(function(node) {
<a name="line603"></a>        node.count--;
<a name="line604"></a>        return node.parent;
<a name="line605"></a>      }, r);
<a name="line606"></a>
<a name="line607"></a>      if (r != node.left) {
<a name="line608"></a>        r.parent.right = r.left;
<a name="line609"></a>        if (r.left) r.left.parent = r.parent;
<a name="line610"></a>        r.left = node.left;
<a name="line611"></a>        r.left.parent = r;
<a name="line612"></a>        b = r.parent;
<a name="line613"></a>      }
<a name="line614"></a>      r.parent = node.parent;
<a name="line615"></a>      r.right = node.right;
<a name="line616"></a>      if (r.right) r.right.parent = r;
<a name="line617"></a>      if (node == this.maxNode_) this.maxNode_ = r;
<a name="line618"></a>      r.count = node.count;
<a name="line619"></a>    } else {
<a name="line620"></a>      r = this.getMinNode_(node.right);
<a name="line621"></a>
<a name="line622"></a>      // Update counts.
<a name="line623"></a>      this.traverse_(function(node) {
<a name="line624"></a>        node.count--;
<a name="line625"></a>        return node.parent;
<a name="line626"></a>      }, r);
<a name="line627"></a>
<a name="line628"></a>      if (r != node.right) {
<a name="line629"></a>        r.parent.left = r.right;
<a name="line630"></a>        if (r.right) r.right.parent = r.parent;
<a name="line631"></a>        r.right = node.right;
<a name="line632"></a>        r.right.parent = r;
<a name="line633"></a>        b = r.parent;
<a name="line634"></a>      }
<a name="line635"></a>      r.parent = node.parent;
<a name="line636"></a>      r.left = node.left;
<a name="line637"></a>      if (r.left) r.left.parent = r;
<a name="line638"></a>      if (node == this.minNode_) this.minNode_ = r;
<a name="line639"></a>      r.count = node.count;
<a name="line640"></a>    }
<a name="line641"></a>
<a name="line642"></a>    // Update the parent of the node being removed to point to its replace
<a name="line643"></a>    if (node.isLeftChild()) {
<a name="line644"></a>      node.parent.left = r;
<a name="line645"></a>    } else if (node.isRightChild()) {
<a name="line646"></a>      node.parent.right = r;
<a name="line647"></a>    } else {
<a name="line648"></a>      this.root_ = r;
<a name="line649"></a>    }
<a name="line650"></a>
<a name="line651"></a>    // Balance the tree
<a name="line652"></a>    this.balance_(b ? b : r);
<a name="line653"></a>  } else {
<a name="line654"></a>    // Update counts.
<a name="line655"></a>    this.traverse_(function(node) {
<a name="line656"></a>      node.count--;
<a name="line657"></a>      return node.parent;
<a name="line658"></a>    }, node.parent);
<a name="line659"></a>
<a name="line660"></a>    // If the node is a leaf, remove it and balance starting from its parent
<a name="line661"></a>    if (node.isLeftChild()) {
<a name="line662"></a>      this.special = 1;
<a name="line663"></a>      node.parent.left = null;
<a name="line664"></a>      if (node == this.minNode_) this.minNode_ = node.parent;
<a name="line665"></a>      this.balance_(node.parent);
<a name="line666"></a>    } else if (node.isRightChild()) {
<a name="line667"></a>      node.parent.right = null;
<a name="line668"></a>      if (node == this.maxNode_) this.maxNode_ = node.parent;
<a name="line669"></a>      this.balance_(node.parent);
<a name="line670"></a>    } else {
<a name="line671"></a>      this.clear();
<a name="line672"></a>    }
<a name="line673"></a>  }
<a name="line674"></a>};
<a name="line675"></a>
<a name="line676"></a>
<a name="line677"></a>/**
<a name="line678"></a> * Returns the node in the tree that has k nodes before it in an in-order
<a name="line679"></a> * traversal, optionally rooted at {@code opt_rootNode}.
<a name="line680"></a> *
<a name="line681"></a> * @param {number} k The number of nodes before the node to be returned in an
<a name="line682"></a> *     in-order traversal, where 0 &lt;= k &lt; root.count.
<a name="line683"></a> * @param {goog.structs.AvlTree.Node=} opt_rootNode Optional root node.
<a name="line684"></a> * @return {goog.structs.AvlTree.Node} The node at the specified index.
<a name="line685"></a> * @private
<a name="line686"></a> */
<a name="line687"></a>goog.structs.AvlTree.prototype.getKthNode_ = function(k, opt_rootNode) {
<a name="line688"></a>  var root = opt_rootNode || this.root_;
<a name="line689"></a>  var numNodesInLeftSubtree = root.left ? root.left.count : 0;
<a name="line690"></a>
<a name="line691"></a>  if (k &lt; numNodesInLeftSubtree) {
<a name="line692"></a>    return this.getKthNode_(k, root.left);
<a name="line693"></a>  } else if (k == numNodesInLeftSubtree) {
<a name="line694"></a>    return root;
<a name="line695"></a>  } else {
<a name="line696"></a>    return this.getKthNode_(k - numNodesInLeftSubtree - 1, root.right);
<a name="line697"></a>  }
<a name="line698"></a>};
<a name="line699"></a>
<a name="line700"></a>
<a name="line701"></a>/**
<a name="line702"></a> * Returns the node with the smallest value in tree, optionally rooted at
<a name="line703"></a> * {@code opt_rootNode}.
<a name="line704"></a> *
<a name="line705"></a> * @param {goog.structs.AvlTree.Node=} opt_rootNode Optional root node.
<a name="line706"></a> * @return {goog.structs.AvlTree.Node} The node with the smallest value in
<a name="line707"></a> *     the tree.
<a name="line708"></a> * @private
<a name="line709"></a> */
<a name="line710"></a>goog.structs.AvlTree.prototype.getMinNode_ = function(opt_rootNode) {
<a name="line711"></a>  if (!opt_rootNode) {
<a name="line712"></a>    return this.minNode_;
<a name="line713"></a>  }
<a name="line714"></a>
<a name="line715"></a>  var minNode = opt_rootNode;
<a name="line716"></a>  this.traverse_(function(node) {
<a name="line717"></a>    var retNode = null;
<a name="line718"></a>    if (node.left) {
<a name="line719"></a>      minNode = node.left;
<a name="line720"></a>      retNode = node.left;
<a name="line721"></a>    }
<a name="line722"></a>    return retNode; // If null, we&#39;ll stop traversing the tree
<a name="line723"></a>  }, opt_rootNode);
<a name="line724"></a>
<a name="line725"></a>  return minNode;
<a name="line726"></a>};
<a name="line727"></a>
<a name="line728"></a>
<a name="line729"></a>/**
<a name="line730"></a> * Returns the node with the largest value in tree, optionally rooted at
<a name="line731"></a> * opt_rootNode.
<a name="line732"></a> *
<a name="line733"></a> * @param {goog.structs.AvlTree.Node=} opt_rootNode Optional root node.
<a name="line734"></a> * @return {goog.structs.AvlTree.Node} The node with the largest value in
<a name="line735"></a> *     the tree.
<a name="line736"></a> * @private
<a name="line737"></a> */
<a name="line738"></a>goog.structs.AvlTree.prototype.getMaxNode_ = function(opt_rootNode) {
<a name="line739"></a>  if (!opt_rootNode) {
<a name="line740"></a>    return this.maxNode_;
<a name="line741"></a>  }
<a name="line742"></a>
<a name="line743"></a>  var maxNode = opt_rootNode;
<a name="line744"></a>  this.traverse_(function(node) {
<a name="line745"></a>    var retNode = null;
<a name="line746"></a>    if (node.right) {
<a name="line747"></a>      maxNode = node.right;
<a name="line748"></a>      retNode = node.right;
<a name="line749"></a>    }
<a name="line750"></a>    return retNode; // If null, we&#39;ll stop traversing the tree
<a name="line751"></a>  }, opt_rootNode);
<a name="line752"></a>
<a name="line753"></a>  return maxNode;
<a name="line754"></a>};
<a name="line755"></a>
<a name="line756"></a>
<a name="line757"></a>
<a name="line758"></a>/**
<a name="line759"></a> * Constructs an AVL-Tree node with the specified value. If no parent is
<a name="line760"></a> * specified, the node&#39;s parent is assumed to be null. The node&#39;s height
<a name="line761"></a> * defaults to 1 and its children default to null.
<a name="line762"></a> *
<a name="line763"></a> * @param {*} value Value to store in the node.
<a name="line764"></a> * @param {goog.structs.AvlTree.Node=} opt_parent Optional parent node.
<a name="line765"></a> * @constructor
<a name="line766"></a> */
<a name="line767"></a>goog.structs.AvlTree.Node = function(value, opt_parent) {
<a name="line768"></a>  /**
<a name="line769"></a>   * The value stored by the node.
<a name="line770"></a>   *
<a name="line771"></a>   * @type {*}
<a name="line772"></a>   */
<a name="line773"></a>  this.value = value;
<a name="line774"></a>
<a name="line775"></a>  /**
<a name="line776"></a>   * The node&#39;s parent. Null if the node is the root.
<a name="line777"></a>   *
<a name="line778"></a>   * @type {goog.structs.AvlTree.Node}
<a name="line779"></a>   */
<a name="line780"></a>  this.parent = opt_parent ? opt_parent : null;
<a name="line781"></a>
<a name="line782"></a>  /**
<a name="line783"></a>   * The number of nodes in the subtree rooted at this node.
<a name="line784"></a>   *
<a name="line785"></a>   * @type {number}
<a name="line786"></a>   */
<a name="line787"></a>  this.count = 1;
<a name="line788"></a>};
<a name="line789"></a>
<a name="line790"></a>
<a name="line791"></a>/**
<a name="line792"></a> * The node&#39;s left child. Null if the node does not have a left child.
<a name="line793"></a> *
<a name="line794"></a> * @type {goog.structs.AvlTree.Node?}
<a name="line795"></a> */
<a name="line796"></a>goog.structs.AvlTree.Node.prototype.left = null;
<a name="line797"></a>
<a name="line798"></a>
<a name="line799"></a>/**
<a name="line800"></a> * The node&#39;s right child. Null if the node does not have a right child.
<a name="line801"></a> *
<a name="line802"></a> * @type {goog.structs.AvlTree.Node?}
<a name="line803"></a> */
<a name="line804"></a>goog.structs.AvlTree.Node.prototype.right = null;
<a name="line805"></a>
<a name="line806"></a>
<a name="line807"></a>/**
<a name="line808"></a> * The height of the tree rooted at this node.
<a name="line809"></a> *
<a name="line810"></a> * @type {number}
<a name="line811"></a> */
<a name="line812"></a>goog.structs.AvlTree.Node.prototype.height = 1;
<a name="line813"></a>
<a name="line814"></a>
<a name="line815"></a>/**
<a name="line816"></a> * Returns true iff the specified node has a parent and is the right child of
<a name="line817"></a> * its parent.
<a name="line818"></a> *
<a name="line819"></a> * @return {boolean} Whether the specified node has a parent and is the right
<a name="line820"></a> *    child of its parent.
<a name="line821"></a> */
<a name="line822"></a>goog.structs.AvlTree.Node.prototype.isRightChild = function() {
<a name="line823"></a>  return !!this.parent &amp;&amp; this.parent.right == this;
<a name="line824"></a>};
<a name="line825"></a>
<a name="line826"></a>
<a name="line827"></a>/**
<a name="line828"></a> * Returns true iff the specified node has a parent and is the left child of
<a name="line829"></a> * its parent.
<a name="line830"></a> *
<a name="line831"></a> * @return {boolean} Whether the specified node has a parent and is the left
<a name="line832"></a> *    child of its parent.
<a name="line833"></a> */
<a name="line834"></a>goog.structs.AvlTree.Node.prototype.isLeftChild = function() {
<a name="line835"></a>  return !!this.parent &amp;&amp; this.parent.left == this;
<a name="line836"></a>};
</pre>


</body>
</html>
